<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------
namespace app\erp\behavior\paySuccessOrder_after;

/**
 *  店铺库存更新
 * @package app\user_center\behavior\user
 * @author shiqiang
 */
class updateStock{
    /**
     * 执行行为 run方法是Behavior唯一的接口
     * @access public
     * @return void
     */
    public function run($params){
		$orderid = $params['orderid'];
		
		$map = [];
		$map['ordercode'] = $orderid;
		$rel = model('ErpGoodsOrder') -> where($map) -> find();
		$store_id = $rel -> store_id;
		$order_type = $rel -> order_type;
		//先  取出 门店对应的仓库 id
		$warehouse_list = model('erp/ErpStoreWarehouse') -> getStoreWarehouse($store_id);
		foreach ($warehouse_list['data'] as $key => $value) {
			if($value['type'] == 1){//分店仓库
				$store_warehouse_list = $value['wid'];
			}
		}

		foreach ($rel -> get_goods_order_skulist as $key => $value) {
			$map = [];
			$map['sku_id'] = $value['sku_id'];
			$map['warehouse_id'] = $store_warehouse_list;
			model('ErpGoodsWarehouseStockNum') ->where($map) -> setDec('frozen_num', $value['num']);//冻结减去
			model('ErpGoodsWarehouseStockNum') ->where($map) -> setDec('stock_num', $value['num']);//总库存减去

			//判断是否是团购订单
			if($order_type ==1){
				$map = [];
				$map['id'] = $value['sponsor_id'];
				model('ErpGroupSponsorSku') ->where($map) -> setDec('dj_num', $value['num']);//冻结减去
            }
		}
		
		
		
    }
}
